package main

import (
	"fmt"
	"sync"
	"time"
)

func worker(id int) {
	fmt.Printf("worker %d starting work\n", id)
	time.Sleep(time.Second)
	fmt.Printf("worker %d done\n", id)
}

func test1() {
	var wg sync.WaitGroup
	size := 10
	wg.Add(size)

	for i := 0; i < size; i++ {
		go func() {
			defer wg.Done()
			worker(i)
		}()
	}
	wg.Wait()
	fmt.Println("test1 end")
}

func test2() {
	var wg sync.WaitGroup
	size := 10
	wg.Add(size)
	for i := 0; i < size; i++ {
		go func(id int) {
			defer wg.Done()
			worker(id)
		}(i)
	}
	wg.Wait()
	fmt.Println("test2 end")
}

func test3() {
	var wg sync.WaitGroup
	size := 10
	wg.Add(size)
	for i := 0; i < size; i++ {
		i := i
		go func() {
			defer wg.Done()
			worker(i)
		}()
	}
	wg.Wait()
	fmt.Println("test3 end")
}

// func main() {
// 	test1()
// 	fmt.Println("main end1")
// 	fmt.Println()
// 	test2()
// 	fmt.Println("main end2")
// 	fmt.Println()
// 	test3()
// 	fmt.Println("main end3")
// 	fmt.Println()
// }
